import {
  IFile,
  IFileBody,
  IFileResponse,
  IResult,
  IStsToken,
  IVersion,
} from "../interfaces";
import { instance } from "../utils";

/** stsToken */
export const fetchStsToken = (
  spaceId: string,
  channelId: string,
  accessKey?: string
): Promise<IResult<IStsToken>> => {
  if (!accessKey || accessKey === "undefined") {
    return instance
      .get(`/space/${spaceId}/channel/${channelId}/stsToken`)
      .then(({ data }) => data);
  }
  return instance
    .get(
      `/space/${spaceId}/channel/${channelId}/stsToken?accessKey=${accessKey}`
    )
    .then(({ data }) => data);
};

/** stsToken */
export const fetchDetailStsToken = (
  spaceId: string
): Promise<IResult<IStsToken>> => {
  return instance.get(`/space/${spaceId}/stsToken`).then(({ data }) => data);
};

/** 获取文件和文件夹 */
export const fetchFiles = (
  spaceId: string,
  channelId: string,
  dirId: string,
  accessKey: string,
  body: IFileBody
): Promise<IResult<IFileResponse>> => {
  return instance
    .post(
      `/space/${spaceId}/channel/${channelId}/dir/${dirId}/file?accessKey=${accessKey}`,
      body
    )
    .then(({ data }) => data);
};

/** 获取文件版本 */
export const fetchFileVersions = (
  spaceId: string = "",
  channelId: string = "",
  fileId: string = "",
  accessKey?: string
): Promise<IResult<IVersion[]>> => {
  if (!accessKey || accessKey === "undefined") {
    return instance
      .get(`/space/${spaceId}/channel/${channelId}/file/${fileId}`)
      .then(({ data }) => data);
  }
  return instance
    .get(
      `/space/${spaceId}/channel/${channelId}/file/${fileId}?accessKey=${accessKey}`
    )
    .then(({ data }) => data);
};
